<template>
    <div class="body_background" :style="GetWindowBackground">
        <div class="body_background padding-top10 ">
            <div class="block-box items-start">
                <div class="big-font-size three-color">发货信息</div>
                <ul class="create-box">
                    <li>
                        <div class="big-font-size main-color">商品类型：</div>
                        <div class="radio-box">
                            <mt-radio
                                    v-model="goodsType"
                                    :options="goodsTypeArr">
                            </mt-radio>
                        </div>
                    </li>
                    <li>
                        <div class="big-font-size main-color">发货单号：</div>
                        <select v-model="goodsOrderSelect" @change="getSelected">
                            <option :value="item.id" v-for="item in goodsOrderList">{{item.name}}</option>
                        </select>
                    </li>
                </ul>
            </div>
            <div class="block-box items-start order-list-box">
                <div class="big-font-size three-color">商品清单</div>
                <div class="order-list-item">
                    <div class="order-content-box" v-for="(item,index) in goodsList" :key="item.id">
                        <div class="big-small-circular" :class="item.choseFlag==true?'background':''"
                             @click="choseGoodsFun(item)"
                        >
                            <div class="small-circular" v-if="item.choseFlag==true"></div>
                        </div>
                        <img :src="item.imgUrl"/>
                        <div class="order-info-box">
                            <div class="main-color big-font-size">{{item.title}}</div>
                            <div class="goods-content">
                                <div>
                                    <div class="three-color small-font-size">分类：蔬菜</div>
                                    <div class="three-color small-font-size">规格：200g</div>
                                    <div class="three-color small-font-size">库存：200袋</div>
                                    <div class="three-color small-font-size">货值：￥2020</div>
                                </div>
                                <!--购物车加减号-->
                                <div class="shop-cart-box">
                                    <div class="shop-cart-button green-color"
                                         @click="minusGoodsFun(item)"
                                    >-
                                    </div>
                                    <input type="number"
                                           v-model="item.num"
                                           class="shop-cart-input"
                                           @blur="inputBlurFun(item,item.num)"
                                    />
                                    <div class="shop-cart-button background"
                                         @click="addGoodsFun(item,false)"
                                    >+
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="block-box logistics-box marBottom">
                <ul class="logistics-ul">
                    <li>
                        <div class="big-font-size main-color supplier-title white-space">物流公司：</div>
                        <div class="three-color small-font-size">顺丰</div>
                    </li>
                    <li>
                        <div class="big-font-size main-color supplier-title white-space">物流单号：</div>
                        <div class="three-color small-font-size">3663465</div>
                    </li>
                </ul>
            </div>
        </div>
        <div class="body_background create-button-box">
            <mt-button class="width40 middle-font-size"
                       @click="cancelClickFun"
            >取消
            </mt-button>
            <mt-button class="width40 middle-font-size background white-color"
                       @click="createInventoryFun"
            >提交
            </mt-button>
        </div>
    </div>
</template>

<script>
    import {Toast, MessageBox} from 'mint-ui';

    export default {
        name: "WarehoueCreate",
        data() {
            return {
                GetWindowBackground: {
                    'min-height': ''
                },
                goodsTypeArr: [
                    {
                        label: '总部直供',
                        value: '1'
                    }, {
                        label: '门店自采',
                        value: '2'
                    }
                ],
                goodsType: '',
                goodsList: [],
                copyGoodsList: [],
                goodsOrderList: [
                    {
                        id: '1',
                        name: '6416564646655'
                    },
                    {
                        id: '2',
                        name: '5465464646566'
                    }
                ],
                goodsOrderSelect: '',
                isSubmitFlag: false,//是否点击过不将差值生成退款单  false:否 true：是
            }
        },
        mounted() {
            this.getNewGoodsList();//重新整合商品数据
            //如果没有这句代码，select中初始化会是空白的，默认选中就无法实现
            this.goodsOrderSelect = this.goodsOrderList[0].id;
            this.goodsType = this.goodsTypeArr[1].value;
        },
        methods: {

            //重新整合商品数据
            getNewGoodsList() {
                var list = [
                    {
                        id: 1,
                        title: "秘制小番茄500g",
                        spec: '500g/袋',
                        weight: '500g',
                        num: 12,
                        imgUrl:
                            "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
                    },
                    {
                        id: 2,
                        title: "秘制小龙虾1500g",
                        spec: '500g/袋',
                        weight: '500g',
                        num: 1,
                        imgUrl:
                            "https://t00img-c.yangkeduo.com/goods/images/2020-08-13/ce99a1b2f665ba478d9573c314686c5d.jpeg"
                    },
                ];
                for (var i = 0; i < list.length; i++) {
                    var item = list[i]
                    item['choseFlag'] = item.num > 0 ? true : false;
                }
                this.goodsList = list
                this.copyGoodsList = JSON.parse(JSON.stringify(list))
            },

            //获取选中
            getSelected() {
                console.log(this.goodsOrderSelect)
            },

            //选商品
            choseGoodsFun(item) {
                item.choseFlag = !item.choseFlag
                item.num = item.choseFlag == true ? 1 : 0
            },

            //input失焦时间（商品数量填写失焦）
            inputBlurFun(item, num) {
                this.addGoodsFun(item, num)
            },

            //选商品
            minusGoodsFun(item) {
                if (item.num <= 0) {
                    Toast('不能再减啦')
                    return false
                }
                if (item.num == 1) {
                    item.choseFlag = false
                    item.num = --item.num
                } else {
                    item.num = --item.num
                }
            },

            //选商品
            addGoodsFun(item, num) {
                item.num = num == false ? ++item.num : Number(num)
                item.choseFlag = true
            },

            //取消
            cancelClickFun() {
                this.$router.push('/StockGoodsManage')
            },

            //提交
            createInventoryFun() {
                var list = this.goodsList;
                var copyGoodsList = this.copyGoodsList;
                var arr = [];
                var flag = false //入库数量少于发货单数量，是否将差值生成退款单
                for (var i = 0; i < list.length; i++) {
                    let item = list[i]
                    if (item.choseFlag) {
                        arr.push({
                            id: item.id,
                            num: item.num
                        })
                    }
                }
                for (var a = 0; a < arr.length; a++) {
                    let item = arr[a];
                    for (var j = 0; j < copyGoodsList.length; j++) {
                        var jItem = copyGoodsList[j]
                        if (item.id == jItem.id) {
                            if (item.num < jItem.num) {
                                flag = true
                            }
                        }
                    }
                }
                if (!this.isSubmitFlag) { //是否点击过不将差值生成退款单  false:否 true：是
                    if (flag) { //是否将差值生成退款单
                        MessageBox.confirm('入库数量少于发货单数量，是否将差值生成退款单?').then(action => {
                            this.ajaxSubmitFun(arr, 0)
                            // this.$router.push('/GoodsRefundSub')
                            return
                        }, () => { //取消
                            this.isSubmitFlag = true  //点击过不生成退款单
                            // this.goodsList = JSON.parse(JSON.stringify(copyGoodsList))
                        });
                    } else {
                        this.ajaxSubmitFun(arr, 1)
                    }
                } else { //直接进行发货，差值不转换退款单
                    this.ajaxSubmitFun(arr, 1)
                }
            },

            //提交的ajax-需要提交的接口
            ajaxSubmitFun(data, type) { // type -- 0:跳转退款单 GoodsRefundSub    1：跳转库存管理  StockGoodsManage
                var url = type == 1 ? '/StockGoodsManage' : '/GoodsRefundSub';
                console.log(data, type, url)
                this.$router.push(url)
            },

            getHeight() {
                // 获取浏览器高度
                this.GetWindowBackground['min-height'] = window.innerHeight + 'px'
            },
        },
        // 生命周期 - 创建完成（可以访问当前this实例）
        created() {
            window.addEventListener('resize', this.getHeight) // 注册监听器
            this.getHeight() // 页面创建时先调用一次
        },
        // 生命周期 - 销毁完成
        destroyed() {
            window.removeEventListener('resize', this.getHeight)
        },
    }
</script>

<style lang="scss" scoped>

    .create-box {
        width: 100%;
        margin-top: 5px;
        li {
            width: 100%;
            height: 45px;
            display: flex;
            align-items: flex-end;
            div {
                margin-bottom: 7px;
            }
            select {
                width: calc(100% - 100px);
                height: 30px;
                border-top: 0;
                border-left: 0;
                border-right: 0;
                border-bottom: 1px solid #ebedf1;
                outline: none;
                list-style: none;
            }
            .radio-box {
                margin-top: auto;
            }
        }
    }

    .order-list-box {
        margin: 10px auto;
        .order-list-item {
            width: 100%;
            background: #fff;
            margin-top: 10px;
            border-radius: 4px;
            .order-top-box {
                width: 100%;
                height: 35px;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .order-content-box {
                width: 100%;
                display: flex;
                align-items: center;
                padding: 7px 0;
                img {
                    width: 80px;
                    height: 80px;
                    margin-right: 10px;
                    border-radius: 4px;
                }
                .goods-content {
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                }
                .order-info-box {
                    width: calc(100% - 110px);
                    position: relative;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    text-align: left;
                }
                .goods-count {
                    margin-left: auto;
                    margin-top: 5px;
                }
            }
            .big-small-circular {
                width: 15px;
                height: 15px;
                border-radius: 50%;
                border: 1px solid #05c569;
                display: flex;
                justify-content: center;
                align-items: center;
                margin-right: 10px;
                .small-circular {
                    width: 7px;
                    height: 7px;
                    border-radius: 50%;
                    background: #fff;
                }
            }
        }
    }

    .order-content-div {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        border-top: 1px dashed #D2D6DC;
        padding-top: 5px;
        margin-top: 15px;
        font-size: 15px;
        .content-item {
            width: 100%;
            min-height: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }

    .logistics-box {
        padding: 10px 20px !important;
        .logistics-ul {
            width: 100%;
            li {
                width: 100%;
                display: flex;
                justify-content: space-between;
                padding: 5px 0;
                .supplier-title {
                    width: 75px;
                    text-align: right;
                }
            }
        }
    }

    .create-button-box {
        width: 100%;
        height: 70px;
        position: fixed;
        bottom: 0px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        .width40 {
            width: 40%;
            border: 1px solid #05c569;
            color: #05c569;
            border-radius: 10px;
        }
    }

    .shop-cart-box {
        position: absolute;
        bottom: 30px;
        right: 0px;
        display: flex;
    }


</style>